﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ScsmIncidentLibrary
{
    public class ConfigMgr
    {
        virtual public string ServiceManagerURL()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerURL"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "localhost";

            return appSettingValue;
        }

        virtual public string ServiceManagerIncidentManagementLibraryMgmtPackGuid()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerIncidentManagementLibraryMgmtPackGuid"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "7B6D701A-4598-8220-DEA8-4AE0504F6F58";

            return appSettingValue;
        }

        virtual public string ServiceManagerSystemWorkItemLibraryMgmtPackGuid()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerSystemWorkItemLibraryMgmtPackGuid"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "405d5590-b45f-1c97-024f-24338290453e";

            return appSettingValue;
        }

        virtual public string ServiceManagerSystemWorkItemIncidentLibraryMgmtPackGuid()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerSystemWorkItemIncidentLibraryMgmtPackGuid"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "dd26c521-7c2d-58c0-0980-dac2dacb0900";

            return appSettingValue;
        }

        virtual public string ServiceManagerSystemWorkItemIncidentProjectionTypeGuid()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerSystemWorkItemIncidentProjectionTypeGuid"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "285CB0A2-F276-BCCB-563E-BB721DF7CDEC";

            return appSettingValue;
        }

        virtual public string ServiceManagerSystemWorkItemIncidentClassGuid()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerSystemWorkItemIncidentClassGuid"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "A604B942-4C7B-2FB2-28DC-61DC6F465C68";

            return appSettingValue;
        }

        virtual public string ServiceManagerMicrosoftADUserClassGuid()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerMicrosoftADUserClassGuid"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "10A7F898-E672-CCF3-8881-360BFB6A8F9A";

            return appSettingValue;
        }

        virtual public string ServiceManagerSystemWorkItemTroubleTicketActionLogClassGuid()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerSystemWorkItemTroubleTicketActionLogClassGuid"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "DBB6A632-0A7E-CEF8-1FC9-405D5CD4D911";

            return appSettingValue;
        }

        virtual public string ServiceManagerSystemWorkItemCreatedByUserRelationshipTypeGuid()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerSystemWorkItemCreatedByUserRelationshipTypeGuid"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "DF738111-C7A2-B450-5872-C5F3B927481A";

            return appSettingValue;
        }

        virtual public string ServiceManagerSystemWorkItemAffectedUserRelationshipTypeGuid()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerSystemWorkItemAffectedUserRelationshipTypeGuid"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "DFF9BE66-38B0-B6D6-6144-A412A3EBD4CE";

            return appSettingValue;
        }

        virtual public string ServiceManagerSystemWorkItemTroubleTicketHasActionLogRelationshipTypeGuid()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerSystemWorkItemTroubleTicketHasActionLogRelationshipTypeGuid"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "A860C62E-E675-B121-F614-E52FCBD9EF2C";

            return appSettingValue;
        }

        virtual public string ServiceManagerContinuedInActionLogMessage()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerContinuedInActionLogMessage"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = " ...continued in action log notes";

            return appSettingValue;
        }

        virtual public string ServiceManagerCreateIncidentIdPrefixFormat()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerCreateIncidentIdPrefixFormat"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "IR_{0}";

            return appSettingValue;
        }

        virtual public string ServiceManagerCreateIncidentSourceEnumGuid()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerCreateIncidentSourceEnumGuid"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "6EF191CE-3124-2974-94FB-020C677F4017";//Portal

            return appSettingValue;
        }

        virtual public string ServiceManagerCreateIncidentStatusEnumGuid()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerCreateIncidentStatusEnumGuid"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "5E2D3932-CA6D-1515-7310-6F58584DF73E";//Active

            return appSettingValue;
        }

        virtual public string ServiceManagerCreateIncidentImpactEnumGuid()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerCreateIncidentImpactEnumGuid"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "80CC222B-2653-2F68-8CEE-3A7DD3B723C1";//Medium

            return appSettingValue;
        }

        virtual public string ServiceManagerCreateIncidentUrgencyEnumGuid()
        {
            string appSettingValue = ConfigurationManager.AppSettings["ServiceManagerCreateIncidentUrgencyEnumGuid"];

            if (string.IsNullOrEmpty(appSettingValue))
                appSettingValue = "02625C30-08C6-4181-B2ED-222FA473280E";//Medium

            return appSettingValue;
        }

        virtual public int ServiceManagerIncidentDescriptionMaxLength()
        {
            return ConvertConfigToInt(
                4000, 
                ConfigurationManager.AppSettings["ServiceManagerIncidentDescriptionMaxLength"]);            
        }

        virtual public int ServiceManagerIncidentTitleMaxLength()
        {
            return ConvertConfigToInt(
                200,
                ConfigurationManager.AppSettings["ServiceManagerIncidentTitleMaxLength"]);
        }

        virtual public int ServiceManagerIncidentActionLogMaxLength()
        {
            return ConvertConfigToInt(
                4000,
                ConfigurationManager.AppSettings["ServiceManagerIncidentActionLogMaxLength"]);
        }

        virtual public string HelpMessage()
        {
            return ConfigurationManager.AppSettings["HelpMessage"];
        }

        private int ConvertConfigToInt(int defaultValue, string appSettingValue)
        {
            int intValue = defaultValue;

            if (!string.IsNullOrEmpty(appSettingValue) &&
                int.TryParse(appSettingValue, out intValue))
            {
                if (intValue < 1 || intValue > 999999)
                {
                    intValue = defaultValue;
                }
            }

            return intValue;
        }
        
        
    }
}
